//
//  AddRecordViewController.m
//  tristana
//
//  Created by 李康 on 2021/1/5.
//  Copyright © 2021 李康. All rights reserved.
//

#import "AddRecordViewController.h"
#import "Otherthings.h"
#import "TSToast.h"
#import "UserService.h"
#import "TSLoginVC.h"


@interface AddRecordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *moneyTF;

@property (weak, nonatomic) IBOutlet UITextField *name1TF;
@property (weak, nonatomic) IBOutlet UITextField *mobileOneTF;
@property (weak, nonatomic) IBOutlet UITextField *nameTwoTF;
@property (weak, nonatomic) IBOutlet UITextField *mobileRTwoTF;
@property (weak, nonatomic) IBOutlet UITextField *timeOneTF;
@property (weak, nonatomic) IBOutlet UITextField *timeTwoTF;

@end

@implementation AddRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.titleString;
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *leftImg = [UIImage imageNamed:@"loginBack"];
    leftBtn.frame = CGRectMake(0, 0, leftImg.size.width, leftImg.size.height);
    [leftBtn setImage:leftImg forState:UIControlStateNormal];

    [leftBtn addTarget:self action:@selector(leftBarAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;


    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGSize size = [@"保存" sizeWithAttributes:@{NSFontAttributeName: GetLightFont(16)}];
    rightBtn.frame = CGRectMake(0, 0, size.width, size.height);
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:HexColor(0x333333) forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBarAction:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.titleLabel.font = GetLightFont(16);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}
 

-(void)leftBarAction:(id)sender{
     [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)addQiantiaoClick:(id)sender {
    AddRecordViewController *vc = [[AddRecordViewController alloc]init];
    vc.titleString = @"";
}
    
-(void)rightBarAction:(id)sender{
    
    if (![UserService isLogin]) {
        [TSLoginVC showLoginVC];
    }else{
        if (IsStrEmpty(self.moneyTF.text) || IsStrEmpty(self.name1TF.text) || IsStrEmpty(self.mobileOneTF.text) || IsStrEmpty(self.nameTwoTF.text)|| IsStrEmpty(self.mobileRTwoTF.text)) {
            [TSToast toast:@"请完整填写信息"];
        }else{
            
            NSDictionary *dataDic = @{ @"money":IsStrEmpty(self.moneyTF.text)?@" ":self.moneyTF.text,
                                        @"nameOne":IsStrEmpty(self.name1TF.text)?@" ":self.name1TF.text,
                                        @"mobileOne":IsStrEmpty(self.mobileOneTF.text)?@" ":self.mobileOneTF.text,
                                        @"nameTwo":IsStrEmpty(self.nameTwoTF.text)?@" ":self.nameTwoTF.text,
                                        @"mobileRTwo":IsStrEmpty(self.mobileRTwoTF.text)?@" ":self.mobileRTwoTF.text,
                                        @"timeOne":IsStrEmpty(self.timeOneTF.text)?@" ":self.timeOneTF.text,
                                        @"timeTwo":IsStrEmpty(self.timeTwoTF.text)?@" ":self.timeTwoTF.text,};
            
            if ([self.titleString isEqualToString:@"添加借条"]) {
                NSMutableArray *arr1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"recordData1"];
                if ( IsArrEmpty(arr1)) {
                    arr1 = [NSMutableArray array];
                }
                [arr1 addObject:dataDic];
                [[NSUserDefaults standardUserDefaults] setObject:arr1 forKey:@"recordData1"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [TSToast toast:@"保存成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self leftBarAction:nil];
                });
            }else{//添加欠条
                NSMutableArray *arr2 = [[NSUserDefaults standardUserDefaults] objectForKey:@"recordData2"];
                if ( IsArrEmpty(arr2)) {
                    arr2 = [NSMutableArray array];
                }
                [arr2 addObject:dataDic];
                
                [[NSUserDefaults standardUserDefaults] setObject:arr2 forKey:@"recordData2"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [TSToast toast:@"保存成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self leftBarAction:nil];
                });
            }
        }
    }
    
    
    
}


@end
